/*
 * @Author: lvye
 * @Description:
 * @Date: 2023-07-03 16:20:11
 * @LastEditTime: 2023-08-22 17:15:44
 * @FilePath: \vue3-ts\src\service\request\index.ts
 */
/*
 * @Author: lvye
 * @Description:
 * @Date: 2023-07-03 16:20:11
 * @LastEditTime: 2023-07-05 10:43:14
 * @FilePath: \vue3-ts\src\service\request\index.ts
 */
import axios from "axios"
import type { AxiosInstance } from "axios"
import { HYRequestInterceptors, HYRequestConfig } from "./type"
import { showMessage } from "./status"
import { openLoading, closeLoading } from "@/hooks/loading"
class HYRequest {
  instance: AxiosInstance
  interceptors?: HYRequestInterceptors
  showLoading: boolean
  constructor(config: HYRequestConfig) {
    this.instance = axios.create(config) //可以创建多个实例  这样baseUrl不一样也可以很好配置
    this.interceptors = config.interceptors
    this.showLoading = config.showLoading ?? true
    // 1.从config中取出的拦截器是对应的实例的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptors,
      this.interceptors?.requestInterceptorsCatch
    )
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
    // 2.添加所有实例都有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        console.log("所有实例请求拦截成功")
        console.log(this.showLoading, "this.showLoading")
        console.log(config, "this.showLoading")
        this.showLoading && openLoading() //请求时调用
        return config
      },
      (err) => {
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        console.log("所有实例响应拦截成功")
        this.showLoading && closeLoading()
        const data = res.data
        if (data?.returnCode === "-1001") {
          console.log("请求失败")
        } else {
          return data
        }
      },
      (err) => {
        const { response } = err
        this.showLoading && closeLoading()
        if (response) {
          // 请求已发出，但是不在2xx的范围
          showMessage(response.status)
          return Promise.reject(response.data)
        }
        console.log("网络连接异常,请稍后再试!")
      }
    )
  }
  request<T>(config: HYRequestConfig): Promise<T> {
    return new Promise((resolve, reject) => {
      // 3.给每个接口添加请求拦截器
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config)
      }
      // 给某些接口不想要loading设置口子
      if (config.showLoading === false) {
        this.showLoading = false
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          // 给每个接口添加响应拦截器
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res)
          }
          // 每次请求完重置loading
          this.showLoading = true
          resolve(res)
        })
        .catch((err) => {
          this.showLoading = true
          reject(err)
        })
    })
  }
  get<T>(config: HYRequestConfig): Promise<T> {
    return this.request({ ...config, method: "GET" })
  }
  post<T>(config: HYRequestConfig): Promise<T> {
    return this.request({ ...config, method: "POST" })
  }
  delete<T>(config: HYRequestConfig): Promise<T> {
    return this.request({ ...config, method: "DELETE" })
  }
}
export default HYRequest
